package cn.bocai.pc.FileMonitorMethod;
import java.io.File;  
import java.util.ArrayList;

import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;  
import org.apache.commons.io.monitor.FileAlterationObserver;  
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.bocai.pc.util.Utils;
  
public class FileListener extends FileAlterationListenerAdaptor{  
	//日志操作
	 private static final Logger logger = LoggerFactory.getLogger(FileListener.class);
    @Override  
    public void onDirectoryChange(File directory) {  
        System.out.println("文件目录变更了:"+directory.getAbsolutePath()); 
        //将目录传给文件夹扫描方法
        queryData(directory.getAbsolutePath());
    }  
  
    @Override  
    public void onDirectoryCreate(File directory) {  
        System.out.println("文件目录创建了:"+directory.getAbsolutePath());  
    }  
  
    @Override  
    public void onDirectoryDelete(File directory) {  
        System.out.println("文件目录删除了:"+directory.getAbsolutePath());  
    }  
  
    @Override  
    public void onFileChange(File file) {  
        System.out.println("文件变更了:"+file.getAbsolutePath());  
    }  
  
    @Override  
    public void onFileCreate(File file) {  
        System.out.println("文件创建了:"+file.getAbsolutePath());  
    }  
  
    @Override  
    public void onFileDelete(File file) {  
        System.out.println("文件删除了:"+file.getAbsolutePath());  
    }  
  
    @Override  
    public void onStart(FileAlterationObserver observer) {  
    	/*  logger.info("文件系统观察者开始检查事件");*/
        System.out.println("开始监听:"+observer.getDirectory());  
    }  
  
    @Override  
    public void onStop(FileAlterationObserver observer) {
    	  /*logger.info("文件系统观察者停止检查事件");*/
        System.out.println("停止监听:"+observer.getDirectory());  
    }  
    
    private void queryData(String path){
    	//扫描出改动路径下数据
    	ArrayList<String> findFileName = findFileName(path);
      //创建cxf服务 
 		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
 		//通过webService服务地址，创建客户端
 		//String variableWSDL=variableService.selectVariable("WS_WSDLMonitor");
 		 Client client = dcf.createClient(Utils.returnSacnDate);
 		if(0==findFileName.size()){
 			findFileName.add("null");//当他为空的时候为了避免传空之报错所以我们在这设置一个null
 		}
 		try {
			client.invoke("returnScanDate",path,findFileName);
		} catch (Exception e) {
			System.out.println("图片端扫描数据无返回！");
		}
    }
    
   	// 查找指定目录下的文件
   	private ArrayList<String> findFileName(String filepath) {
   			File file = new File(filepath);
   			ArrayList<String> list = new ArrayList<String>();
   			if (!file.exists() || !file.isDirectory()) {
   				System.out.println("目录不存在");
   			} else {
   				File[] files = file.listFiles();
   				/*System.err.println("扫描出图片数目："+files.length);*/
   				for (int i = 0; i < files.length; i++) {
   					if (files[i].isFile()) {
   						if (files[i].getName().endsWith(".jpg")) {
   							list.add(files[i].getName());
   							/*System.err.println(files[i].getName());*/
   						}
   					}
   				}
   			}
   			return list;
   		}
}  